package com.chanjiedata.changbs.module.mine;

/**
 * 文件名 : PolicyActivity
 * 创建者 : MangoWang
 * 创建日期 : 2019/12/3 20:54
 * 微信 : MangoWaWang
 * 邮箱 : 763482205@qq.com
 * 描述 : TODO
 */

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chanjiedata.changbs.R;
import com.chanjiedata.changbs.adapter.MyPolicyAdapter;
import com.chanjiedata.changbs.base.BaseActivity;
import com.chanjiedata.changbs.module.main.ShareProfitActivity;
import com.chanjiedata.changbs.net.Apis;
import com.chanjiedata.changbs.net.NetworkManager;
import com.chanjiedata.changbs.net.interfaces.OnRequestCallBack;
import com.chanjiedata.changbs.proto.Mystock;
import com.chanjiedata.changbs.utlis.StatusBarUtil;
import com.google.protobuf.InvalidProtocolBufferException;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class PolicyActivity extends BaseActivity {

    @BindView(R.id.back_tv)
    TextView backTv;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;

    private MyPolicyAdapter mAdapter;
    private List<Mystock.myMachine.List> datas = new ArrayList<>();


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_policy);
        ButterKnife.bind(this);
        StatusBarUtil.setStatusBarLightMode(this, Color.parseColor("#ffffff"), true);
        initRecyclerView();
        initListener();
        tvTitle.setText("我的政策");
        initData();
    }

    private void initListener() {
        backTv.setOnClickListener(view -> finish());

    }

    private void initData() {
        NetworkManager.INSTANCE.post(Apis.myMachine, new OnRequestCallBack() {
            @Override
            public void onOk(byte[] response) throws InvalidProtocolBufferException {
                datas = Mystock.myMachine.parseFrom(response).getListList();
                mAdapter.addData(datas);
                mAdapter.loadMoreEnd();
            }

            @Override
            public void onError(int errorCode, String errorMessage) {
                showToast(errorMessage);
            }
        });


    }

    @Override
    protected void onResume() {
        super.onResume();

    }

    private void initRecyclerView() {

        recyclerView.setLayoutManager(new LinearLayoutManager(mContext));
        recyclerView.setAdapter(mAdapter = new MyPolicyAdapter(mContext, datas));
        mAdapter.bindToRecyclerView(recyclerView);
        mAdapter.setEmptyView(R.layout.recycle_empty);
        // 每个item的点击事件
        mAdapter.setOnItemClickListener((adapter, view, position) -> {
            Intent intent = new Intent(mContext, ShareProfitActivity.class);
            intent.putExtra("goods_id", datas.get(position).getGoodsId());
            intent.putExtra("title", "分润细则");
            startActivity(intent);
        });

    }


}
